var users = require('google/appengine/api/users');
var Response = require('nitro/response').Response;

var check = exports.check = function(params) {
  var response = Response.ok();
  
  var cmdID = params.commandID;
  
  var authStatus = {
    loggedIn: false,
    isAdmin: false,
    character: '',
    commandID: params.commandID
  };
  
  var user = users.getCurrentUser();
  
  if(user) {
    authStatus.loggedIn = true;
    authStatus.character = user.nickname;
    
    if(users.isCurrentUserAdmin()) {
      authStatus.isAdmin = true;
    }
  }
  
  response.body.push(JSON.stringify(authStatus));
  return Response.json(authStatus);
};

exports.POST = function(req) {
  var postData = req.input.read().decodeToString("utf-8");
    
  var params = null;
  if(postData !== '') {
    params = JSON.parse(postData);
  }
  else {
    var cmdID = req.pathInfo.substr(1);
    params = {
      commandID: cmdID
    };
  }
  
  return check(params);
};

exports.GET = function(req) {
  var params = {
    commandID: req.pathInfo.substr(1)
  };

  return check(params);
};